package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.AddressListReq;
import com.ydkj.a37e_mall.Classes.Model.Response.AddressListResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/2/7.
 */

public class ChooseAddress extends BaseActivity {
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private ImageView img_back;
    private TextView tv_title, tv_edit, tv_addNewAdreess, tv_no_address_data;
    private ListView lv_address;
    private List<AddressListResp.ResultEntity.DataEntity> addressInfoList;
    private AddressListAdapter addressListAdapter;
    private String secret;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage_address);
        mActivity = this;
        init();
//        initWindow();//沉浸式状态栏
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.e50011), 0);
    }

    private void init() {

        myApplication = MyApplication.getInstance();
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        tv_title = (TextView) mActivity.findViewById(R.id.tv_title);
        tv_edit = (TextView) mActivity.findViewById(R.id.tv_edit);
        tv_addNewAdreess = (TextView) mActivity.findViewById(R.id.tv_addNewAdreess);
        tv_no_address_data = (TextView) mActivity.findViewById(R.id.tv_no_address_data);
        tv_edit.setVisibility(View.GONE);
        tv_title.setText("选择收货地址");

        addressInfoList = new ArrayList<AddressListResp.ResultEntity.DataEntity>();
        lv_address = (ListView) mActivity.findViewById(R.id.lv_address);
        addressListAdapter = new AddressListAdapter(addressInfoList);
        lv_address.setAdapter(addressListAdapter);
//        GetAddressList();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_addNewAdreess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myApplication.setName1("");
                myApplication.setMobile1("");
                myApplication.setZip_code1("");
                myApplication.setAddress1("");
                myApplication.setProvince1("");
                myApplication.setCity1("");
                myApplication.setArea1("");
                myApplication.setLocation1("");
                myApplication.setDetail1("");
                myApplication.setIs_default1("");
                Intent intent = new Intent(mActivity, AddNewAddress.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    private class AddressListAdapter extends BaseAdapter {
        private List<AddressListResp.ResultEntity.DataEntity> addressInfoList;

        private AddressListAdapter(List<AddressListResp.ResultEntity.DataEntity> infos) {
            if (infos == null) {
                infos = new ArrayList<AddressListResp.ResultEntity.DataEntity>();
            }
            addressInfoList = infos;
        }

        @Override
        public int getCount() {
            return addressInfoList.size();
        }

        /**
         * 获得某一位置的数据
         */
        @Override
        public AddressListResp.ResultEntity.DataEntity getItem(int position) {
            return addressInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.address_listitem, null);
                vh = new ViewHolder();
                vh.ly_confirmation_address = (LinearLayout) convertView.findViewById(R.id.ly_confirmation_address);
                vh.cb_choose = (ImageView) convertView.findViewById(R.id.cb_choose);
                vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                vh.tv_phoneNumber = (TextView) convertView.findViewById(R.id.tv_phoneNumber);
                vh.tv_address = (TextView) convertView.findViewById(R.id.tv_address);
                vh.tv_default = (TextView) convertView.findViewById(R.id.tv_default);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);
            convertView.setClickable(true);

            vh.tv_name.setText(getItem(position).getName());
            vh.tv_phoneNumber.setText(getItem(position).getMobile());
            vh.tv_address.setText(getItem(position).getDetail());
            vh.cb_choose.setVisibility(View.GONE);

            if (getItem(position).getIs_default() == 1) {
                vh.ly_confirmation_address.setBackgroundColor(getResources().getColor(R.color.f1f1f1));
                vh.tv_name.setTextColor(getResources().getColor(R.color.e50011));
                vh.tv_phoneNumber.setTextColor(getResources().getColor(R.color.e50011));
                vh.tv_address.setTextColor(getResources().getColor(R.color.e50011));
                vh.tv_address.setTextSize(14);
                vh.tv_default.setVisibility(View.VISIBLE);
            } else {
                vh.ly_confirmation_address.setBackgroundColor(getResources().getColor(R.color.ffffff));
                vh.tv_name.setTextColor(getResources().getColor(R.color.A333333));
                vh.tv_phoneNumber.setTextColor(getResources().getColor(R.color.A333333));
                vh.tv_address.setTextColor(getResources().getColor(R.color.A333333));
                vh.tv_address.setTextSize(12);
                vh.tv_default.setVisibility(View.GONE);
            }

//            getItem(position).setIs_check(false);
//            final ViewHolder finalVh = vh;
//            vh.img_choose.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (getItem(position).getIs_check() == false) {
//                        finalVh.img_choose.setImageDrawable(getResources().getDrawable(R.mipmap.choose_red));
//                        getItem(position).setIs_check(true);
//
//                    } else {
//                        finalVh.img_choose.setImageDrawable(getResources().getDrawable(R.mipmap.choose_default));
//                        getItem(position).setIs_check(false);
//                    }
//                }
//            });

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ChooseAddress.this, ConfirmationOrder.class);
                    Log.e("url", "id==" + getItem(position).getId());
                    intent.putExtra("name", getItem(position).getName());
                    intent.putExtra("detail", getItem(position).getDetail());
                    intent.putExtra("mobile", getItem(position).getMobile());
                    intent.putExtra("id", getItem(position).getId());
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                }
            });

            return convertView;
        }

        private class ViewHolder {
            private LinearLayout ly_confirmation_address;
            private ImageView img_choose;
            private ImageView cb_choose;
            public TextView tv_name;
            public TextView tv_phoneNumber;
            public TextView tv_address;
            public TextView tv_default;
        }
    }

    //获取收货地址列表
    private void GetAddressList() {
        Log.e("url", "1111111==" + 111111111);
        CustomProgressDialog.createDialog(mActivity).show();
        AddressListReq req = new AddressListReq();

        String url = Const.COMMON_ADDRESS + req.urlString();
//        Log.e("ssss", "myApplication.getDataEntity().getUuid()==>" + myApplication.getDataEntity().getUuid());
        req.setUuid(myApplication.getDataEntity().getUuid());
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", secret);
        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("url", "json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                CustomProgressDialog.Dismiss();
                if (errno.equals("000")) {
                    AddressListResp resp = GsonImpl.get().toObject(response.toString(), AddressListResp.class);
                    addressInfoList.clear();
                    addressInfoList.addAll(resp.getResult().getData());
                    addressListAdapter.notifyDataSetChanged();
                } else if (errno.equals("001")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 3);
                    Log.e("url", "errmsg==" + errmsg);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        GetAddressList();
    }
}
